import { ref, onMounted, watch } from "vue"
import { useRoute } from "vue-router"
import { getCategoryAPI } from "@/apis/category"

export function useCategory() {
  const  categoryData = ref({})
  const route = useRoute()
  const getCategory = async () => {
      const res = await getCategoryAPI(route.params.id)
      categoryData.value = res.result
  }
  onMounted(()=> getCategory()) 

  //路由参数变化时 可以把分类数据接口重新发送
  // onBeforeRouteUpdate((to) => {
  //   console.log('路由变化了');
  //   console.log(to);
    
  //   getCategory(to.params.id)
  // })

  watch(route, () => getCategory())
  return {
    categoryData
  }
}